package com.woniuxy.sale.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.model.entity.AccountManage;
import com.woniuxy.common.service.FeignRechargeService;
import com.woniuxy.model.vo.UpdateSaleMoneyVo;
import com.woniuxy.response.CustomPage;
import com.woniuxy.model.entity.ExportTable;
import com.woniuxy.model.entity.SaleMoney;
import com.woniuxy.sale.entity.dto.SaleMoneyDTO;
import com.woniuxy.sale.entity.vo.*;
import com.woniuxy.sale.exception.SaleMoneyAddException;
import com.woniuxy.sale.mapper.SaleMoneyMapper;
import com.woniuxy.sale.service.ExportTableService;
import com.woniuxy.sale.service.SaleMoneyService;
import com.woniuxy.sale.service.SaleOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 金额结算表; 服务实现类
 * </p>
 *
 * @author lio
 * @since 2023-09-07
 */
@Service
@Slf4j
public class SaleMoneyServiceImpl implements SaleMoneyService {

    @Resource
    private SaleMoneyMapper saleMoneyMapper;

    @Resource
    private SaleOrderService saleOrderService;

    @Resource
    private FeignRechargeService feignRechargeService;

    @Resource
    private ExportTableService exportTableService;

    @Transactional(rollbackFor = SaleMoneyAddException.class)
    @Override
    public boolean add(SaleMoneyVo saleMoneyVo) throws SaleMoneyAddException{

        //通过sno查找客户名称
        String customer = saleOrderService.findCustomerBySno(saleMoneyVo.getSno());
        saleMoneyVo.setCustomer(customer);
        //1.填写金额结算表 类型:定金/尾款 选货单编号 客户名称 总价
        if(!saleMoneyMapper.add(saleMoneyVo)){
            throw new SaleMoneyAddException("金额结算表新增失败,请重新填写");
        }
        return true;
    }

    @Transactional
    @Override
    public boolean updateState(UpdateSaleMoneyVo updateSaleMoneyVo) {
        return saleMoneyMapper.updateState(updateSaleMoneyVo);
    }

    @Override
    public CustomPage<List<SaleMoney>> moneyManage(MoneyManageVo moneyManageVo) {
        PageHelper.startPage(moneyManageVo.getCurrent(),moneyManageVo.getSize());

        List<SaleMoney> salePackList = saleMoneyMapper.managePack(moneyManageVo);
        PageInfo<SaleMoney> pageInfo = new PageInfo<>(salePackList);

        CustomPage<List<SaleMoney>> customPage = CustomPage.<List<SaleMoney>>builder()
                .currentPage(moneyManageVo.getCurrent())
                .size(moneyManageVo.getSize())
                .totalPage(pageInfo.getPages())
                .total(pageInfo.getTotal())
                .data(salePackList)
                .build();
        return customPage;
    }

    @Transactional(rollbackFor = SaleMoneyAddException.class)
    @Override
    public boolean del(Integer id, String operator) {
        //判断金额结算表的状态,只有未提交的才能删除
        String state = saleMoneyMapper.findStateById(id);
        if(!"0".equals(state)){
            throw new SaleMoneyAddException("金额结算表已提交,不能删除!");
        }

        //删除
        if(!saleMoneyMapper.del(id,operator)){
            throw new SaleMoneyAddException("删除金额结算表失败,你没有该权限!");
        }

        return true;
    }

    @GlobalTransactional(rollbackFor = SaleMoneyAddException.class)
    @Override
    public boolean submitSaleMoney(SubmitSaleMoneyVo submitSaleMoneyVo) {

        //1.通过id查找金额结算表信息
        SaleMoney saleMoney = saleMoneyMapper.findAllById(submitSaleMoneyVo.getId());

        if(!saleMoney.getCreator().equals(submitSaleMoneyVo.getOperator())){
            throw new SaleMoneyAddException("金额结算表提交失败,你没有权限!");
        }

        //2.修改金额结算表状态
        UpdateSaleMoneyVo updateSaleMoneyVo = new UpdateSaleMoneyVo();
        updateSaleMoneyVo.setOperator(submitSaleMoneyVo.getOperator())
                        .setState("1").setSno(saleMoney.getSno()).setType(saleMoney.getType());

        if(!saleMoneyMapper.updateState(updateSaleMoneyVo)){
            throw new SaleMoneyAddException("金额结算表提交失败,请重新提交!");
        }

        //3.填写account_manage表 经办人 客户名称 应收金额 创建时间 状态1 sno money_type a_lock
        AccountManage accountManage = new AccountManage();
        //3.1通过uid查找用户账号 经办人
        //String adminName = saleMoneyMapper.findAccountByUid(saleMoneyVo.getOperator());
        accountManage.setAdminName(saleMoney.getOperator());
        //3.2客户名称
        accountManage.setUserName(saleMoney.getCustomer());
        //3.3应收金额
        accountManage.setReceivableMoney(saleMoney.getTotalPrice());
        //3.4创建时间
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String createTime = dateFormat.format(date);
        accountManage.setCreateTime(createTime);
        //3.5状态1 未处理
        accountManage.setStatus("1");
        //3.6 sno
        accountManage.setSno(saleMoney.getSno());
        //3.7 money_type
        accountManage.setMoneyType(saleMoney.getType());
        //3.8 a_lock
        accountManage.setALock("1");

        if(!feignRechargeService.addAccountManage(accountManage).getData()){
            throw new SaleMoneyAddException("账户管理表新增失败,请重新提交");
        }

        return true;
    }

    @Transactional(rollbackFor = SaleMoneyAddException.class)
    @Override
    public boolean updateSaleMoney(SaleMoneyUpdateVo saleMoneyUpdateVo) {

        //1.通过id查找金额结算表信息
        SaleMoney saleMoney = saleMoneyMapper.findAllById(saleMoneyUpdateVo.getId());

        if(!saleMoney.getCreator().equals(saleMoneyUpdateVo.getOperator())){
            throw new SaleMoneyAddException("金额结算表修改失败,你没有权限!");
        }

        if(!"0".equals(saleMoney.getState())){
            throw new SaleMoneyAddException("金额结算表已提交,修改失败!");
        }

        if(!saleMoneyMapper.updateSaleMoney(saleMoneyUpdateVo)){
            throw new SaleMoneyAddException("金额结算表修改失败!");
        }

        return false;
    }

    @Override
    public CustomPage<List<SaleMoneyDTO>> moneyInfo(SaleMoneyInfoVo saleMoneyInfoVo) {
        PageHelper.startPage(saleMoneyInfoVo.getCurrent(),saleMoneyInfoVo.getSize());

        List<SaleMoneyDTO> saleMoneyList = saleMoneyMapper.moneyInfo(saleMoneyInfoVo);
        PageInfo<SaleMoneyDTO> pageInfo = new PageInfo<>(saleMoneyList);

        CustomPage<List<SaleMoneyDTO>> customPage = CustomPage.<List<SaleMoneyDTO>>builder()
                .currentPage(saleMoneyInfoVo.getCurrent())
                .size(saleMoneyInfoVo.getSize())
                .totalPage(pageInfo.getPages())
                .total(pageInfo.getTotal())
                .data(saleMoneyList)
                .build();
        return customPage;
    }

    @Transactional
    @Override
    public boolean exportSaleMoney(ExportTable table) {
        return exportTableService.add(table);
    }
}
